package com.education.welco.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.education.auth.entity.UserInfo;
import com.education.common.poi.POIUtils;
import com.education.common.pojo.AjaxResult;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.StringUtils;
import com.education.utils.JwtUtil;
import com.education.welco.pojo.OrderQuestion;
import com.education.welco.service.OrderQuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.education.common.core.BaseController.writeJSON;


/**
 * 试题表
 *
 * @date 2021-03-04 15:45:19
 */
@RestController
@RequestMapping("/orderquestion")
public class OrderQuestionController {
    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private OrderQuestionService orderQuestionService;

    //添加试题
    @PostMapping("/addquestion")
    public void addquestion(HttpServletResponse response, @RequestBody OrderQuestion orderQuestions) throws IOException {


        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        orderQuestions.setCreateTime(format);
        int question = orderQuestionService.insertOrderQuestion(orderQuestions);
        writeJSON(question, response, "200", "添加成功");

    }

    //修改试题
    @PostMapping("/updatequestion")
    public void updatequestion(HttpServletResponse response, @RequestBody OrderQuestion orderQuestion) throws IOException {
        int question = orderQuestionService.updateOrderQuestion(orderQuestion);
        writeJSON(question, response, "200", "修改成功");
    }

    //查询试题列表
    @GetMapping("/selectlist")
    public AjaxResult selectquestionlist(HttpServletResponse response, OrderQuestion orderQuestion, Integer pageSize, Integer pageNum) throws IOException {
        if (pageSize != null && pageNum != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<OrderQuestion> orderQuestions = orderQuestionService.selectOrderQuestionList(orderQuestion);
        PageInfo<OrderQuestion> pageInfo = new PageInfo<>(orderQuestions);
        return AjaxResult.success(pageInfo);
        //writeJSON(orderQuestions,response,"200","查询成功");
    }

    //删除单个试题
    @GetMapping("/deletequestion")
    public void deletequestion(HttpServletResponse response, int id) throws IOException {
        OrderQuestion orderQuestion = orderQuestionService.selectOrderQuestionById(id);
        if (orderQuestion != null) {
            writeJSON(orderQuestionService.deleteOrderQuestionById(id), response, "200", "删除成功");
        } else {
            writeJSON(null, response, "201", "删除失败");
        }
    }

    //删除多个试题
    @GetMapping("/deletequestions")
    public void deleteOrderQuestionByIds(HttpServletResponse response, String[] ids) throws IOException {
        if (ids != null) {
            writeJSON(orderQuestionService.deleteOrderQuestionByIds(ids), response, "200", "删除成功");
        } else {
            writeJSON(null, response, "201", "删除失败");
        }
    }

    /**
     * 教师端-我的资源-试题资源
     * 试题导入
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/Question/import")
    public ApiReturnObject importQuestion(HttpServletResponse response, MultipartFile file) throws IOException {
        //读取excel数据
        List<String[]> strings = POIUtils.readExcel(file);
        for (String[] string : strings) {
            OrderQuestion questions = new OrderQuestion();
            //Questions questions = new Questions();
            Date myDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            questions.setCreateTime(sdf.format(myDate));
            questions.setStContent(string[0]);
            questions.setStSelecta(string[1]);
            questions.setStSelectb(string[2]);
            questions.setStSelectc(string[3]);
            questions.setStSelectd(string[4]);
            questions.setStSelecte(string[5]);
            questions.setStAnswer(string[6]);
            //String s = StringUtil.isEmpty(string[8]) ? "0" : string[8];
            //questions.setStScore(new BigDecimal(s));
            questions.setLevel(string[7]);
            questions.setQuestionType(string[8]);
            questions.setStanalysis(string[9]);
            questions.setCatalogName(string[10]);

            if (StringUtils.isNotBlank(string[10])) {
                List<Integer> li = orderQuestionService.selectCatalogIdList(string[10]);
                if (CollectionUtil.isNotEmpty(li) && li.size() == 1) {
                    questions.setCatalogId(String.valueOf(li.get(0)));
                }
            }
            UserInfo userInfo = jwtUtil.getInfoFromToken();
            questions.setStCteater(String.valueOf(userInfo.getId()));
            int question = orderQuestionService.insertOrderQuestion(questions);
            //writeJSON(question,response, "200", "添加成功");
        }
        return new ApiReturnObject("200", "添加成功", "");
    }
}
